package com.zmj.tl.database.taos.controller;

import com.zmj.tl.database.taos.domain.MeterMetric;
import com.zmj.tl.database.taos.domain.MeterSchema;
import com.zmj.tl.database.taos.service.MeterService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author hd
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/meter")
public class MeterController {
    private final MeterService meterService;

    @PostMapping(value = "/createTb")
    public ResponseEntity<Integer> createTable(@RequestBody MeterSchema meterSchema) {
        return ResponseEntity.ok(this.meterService.createTable(meterSchema));
    }

    @PostMapping(value = "/createStb")
    public ResponseEntity<Integer> createStable(@RequestBody MeterSchema meterSchema) {
        return ResponseEntity.ok(this.meterService.createStable(meterSchema));
    }

    @PostMapping(value = "/dropTb")
    public void dropTable(@RequestBody MeterSchema meterSchema) {
        this.meterService.dropTable(meterSchema);
    }

    @PostMapping(value = "/dropDb")
    public void dropDatabase(@RequestBody MeterSchema meterSchema) {
        this.meterService.dropDataBase(meterSchema);
    }

    @PostMapping(value = "/addOne")
    public void addOneMeter(@RequestBody MeterMetric resource) {
        resource.setTs(new Date());
        this.meterService.addOne(resource);
    }

    @PostMapping(value = "/addEntity")
    public void addMeterEntity(@RequestBody MeterMetric resource) {
        resource.setTs(new Date());
        this.meterService.addEntity(resource);
    }

    @PostMapping(value = "/addBatch")
    public void addMeters(@RequestBody MeterMetric resource) {
        for (MeterMetric meterMetric : resource.getList()) {
            meterMetric.setTs(new Date());
        }
        this.meterService.addBatch(resource);
    }
}
